﻿/*!	@file	r_math_colorRGBi.cpp
	@brief	整数値色定義
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_math_colorRGBi.h"
#include "r_math_colorRGBf.h"

namespace r{
	namespace math{
		ColorRGBi :: ColorRGBi ( void )
		{
			col = 0;
		}
		ColorRGBi :: ColorRGBi ( r_ui32 color )
		{
			col = color;
		}
		ColorRGBi :: ColorRGBi ( r_ui8 _r, r_ui8 _g, r_ui8 _b, r_ui8 _a )
		{
			r = _r;
			g = _g;
			b = _b;
			a = _a;
		}
		ColorRGBi :: ColorRGBi ( r_ui8* _rgba )
		{
			r = _rgba[0];
			g = _rgba[1];
			b = _rgba[2];
			a = _rgba[3];
		}
		ColorRGBi :: ColorRGBi ( const ColorRGBf &color )
		{
			r_f32 _r[3] = {0, color.r, 1.0f};
			r_f32 _g[3] = {0, color.g, 1.0f};
			r_f32 _b[3] = {0, color.b, 1.0f};
			r_f32 _a[3] = {0, color.a, 1.0f};
			r = static_cast<r_ui8> ( _r[ ( color.r > 0 ) + ( color.r > 1.0f ) ] * 255 );
			g = static_cast<r_ui8> ( _g[ ( color.g > 0 ) + ( color.g > 1.0f ) ] * 255 );
			b = static_cast<r_ui8> ( _b[ ( color.b > 0 ) + ( color.b > 1.0f ) ] * 255 );
			a = static_cast<r_ui8> ( _a[ ( color.a > 0 ) + ( color.a > 1.0f ) ] * 255 );
		}

		r_bool	ColorRGBi :: operator == ( const ColorRGBi &src ) const
		{
			return col == src.col;
		}
		r_bool	ColorRGBi :: operator != ( const ColorRGBi &src ) const
		{
			return col != src.col;
		}

		ColorRGBi	ColorRGBi :: operator + ( const ColorRGBi &src ) const
		{
			r_ui32 _r[2] = {r + src.r, 0xff};
			r_ui32 _g[2] = {g + src.g, 0xff};
			r_ui32 _b[2] = {b + src.b, 0xff};
			r_ui32 _a[2] = {a + src.a, 0xff};
			return ColorRGBi ( static_cast<r_ui8> ( _r[_r[0] > 0xff] ), static_cast<r_ui8> ( _g[_g[0] > 0xff] ), static_cast<r_ui8> ( _b[_b[0] > 0xff] ), static_cast<r_ui8> ( _a[_a[0] > 0xff] ) );
		}

		const ColorRGBi&	ColorRGBi :: operator += ( const ColorRGBi &src )
		{
			r_ui32 _r[2] = {r + src.r, 0xff};
			r_ui32 _g[2] = {g + src.g, 0xff};
			r_ui32 _b[2] = {b + src.b, 0xff};
			r_ui32 _a[2] = {a + src.a, 0xff};
			r = static_cast<r_ui8> ( _r[_r[0] > 0xff] );
			g = static_cast<r_ui8> ( _g[_g[0] > 0xff] );
			b = static_cast<r_ui8> ( _b[_b[0] > 0xff] );
			a = static_cast<r_ui8> ( _a[_a[0] > 0xff] );
			return *this;
		}

		ColorRGBi	ColorRGBi :: operator * ( const ColorRGBi &src ) const
		{
			return ColorRGBi ( ( r * src.r ) >> 8, ( g * src.g ) >> 8, ( b * src.b ) >> 8, ( a * src.a ) >> 8 );
		}

		const ColorRGBi&	ColorRGBi :: operator *= ( const ColorRGBi &src )
		{
			r = ( r * src.r ) >> 8;
			g = ( g * src.g ) >> 8;
			b = ( b * src.b ) >> 8;
			a = ( a * src.a ) >> 8;
			return *this;
		}

		ColorRGBi ColorRGBi :: operator + ( r_ui8 src ) const
		{
			r_ui32 _r[2] = {r + src, 0xff};
			r_ui32 _g[2] = {g + src, 0xff};
			r_ui32 _b[2] = {b + src, 0xff};
			r_ui32 _a[2] = {a + src, 0xff};
			return ColorRGBi ( static_cast<r_ui8> ( _r[_r[0] > 0xff] ), static_cast<r_ui8> ( _g[_g[0] > 0xff] ), static_cast<r_ui8> ( _b[_b[0] > 0xff] ), static_cast<r_ui8> ( _a[_a[0] > 0xff] ) );
		}

		const ColorRGBi&	ColorRGBi :: operator += ( r_ui8 src )
		{
			r_ui32 _r[2] = {r + src, 0xff};
			r_ui32 _g[2] = {g + src, 0xff};
			r_ui32 _b[2] = {b + src, 0xff};
			r_ui32 _a[2] = { + src, 0xff};
			r = static_cast<r_ui8> ( _r[_r[0] > 0xff] );
			g = static_cast<r_ui8> ( _g[_g[0] > 0xff] );
			b = static_cast<r_ui8> ( _b[_b[0] > 0xff] );
			a = static_cast<r_ui8> ( _a[_a[0] > 0xff] );
			return *this;
		}

		ColorRGBi ColorRGBi :: operator * ( r_ui8 src ) const
		{
			return ColorRGBi ( ( r * src ) >> 8, ( g * src ) >> 8, ( b * src ) >> 8, ( a * src ) >> 8 );
		}
		const ColorRGBi&	ColorRGBi :: operator *= ( r_ui8 src )
		{
			r = ( r * src ) >> 8;
			g = ( g * src ) >> 8;
			b = ( b * src ) >> 8;
			a = ( a * src ) >> 8;
			return *this;
		}
	}
}
